package com.example.demo.controller;

import com.example.demo.pojo.Module;
import com.example.demo.pojo.PageResult;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.Role;
import com.example.demo.service.Impl.RoleServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class RoleController {
    @Autowired
    RoleServiceImpl roleService;

    // 添加
    @PostMapping("/role/add")
    public Result add(@RequestBody Role role){
        roleService.add(role);
        return Result.success("成功");
    }
    // 显示
    @GetMapping("/role/list")
    public Result list(@RequestParam int page,
                       @RequestParam int limit) {

        PageInfo list = roleService.queryEnchanter(page, limit);
        return PageResult.success03(list.getList(), (int) list.getTotal());
    }
    // 修改
    @PutMapping("/role/edit")
    public Result edit(@RequestBody Role role){
        roleService.update(role);
        return Result.success("修改成功");
    }

    // 删除
    @DeleteMapping("/role/del")
    public Result del(@RequestParam int id){
        boolean del = roleService.del(id);
        if (del){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
    // 列表
    @GetMapping("/role/module")
    public Map<String, Object> module(@RequestParam String role){
        HashMap<String, Object> map = new HashMap<>();
        String idlist=roleService.idlist(role).getModule();
        if (idlist.equals("all")){
            idlist= roleService.getlevelAll();
        }
        List<Module> level=roleService.getlevel(idlist);
        System.out.println(level);
        map.put("code",200);
        map.put("msg","获取成功");
        map.put("idList",idlist);
        map.put("data",level);
        return map;
    }


}
